//
//  YZMStockSettlementCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/29.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockSettlementCell.h"
#import "YZMTradeSettlementModel.h"
#import "NSNumber+Utils.h"
#import "UILabel+Color.h"

@interface YZMStockSettlementCell()

@property (nonatomic, strong) UILabel *nameLB;
@property (nonatomic, strong) UILabel *amountLB;
@property (nonatomic, strong) UILabel *totalMoneyLB;
@property (nonatomic, strong) UILabel *resultStringLB;
@property (nonatomic, strong) UILabel *resultMoneyLB;

@property (nonatomic, strong) UILabel *freeExLB;

@property (nonatomic, strong) UIView *leftHelperView;

@end


@implementation YZMStockSettlementCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        [self createObjects];
        self.rightMargin = 0.0;
    }
    
    return self;
}

- (void)setRecord:(YZMTradeSettlementModel *)record{
    
    self.nameLB.text = record.stockName;
    if (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        self.amountLB.text = [NSString stringWithFormat:@"%@股",@(record.holdNum)];
        if ([record.profitFree isGreaterThanDouble:0]) {
            [_leftHelperView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(30);
                make.centerY.equalTo(self.contentView).with.offset(-20);
            }];
            self.totalMoneyLB.text = [@"+" stringByAppendingString:record.profitFree.p02fString];
            self.totalMoneyLB.textColor=[YZMAppStyle colorForRiseText];
            self.freeExLB.hidden = NO;
            self.freeExLB.text = [NSString stringWithFormat:@"本次免费体验活动所获得的红包金额%@元已发放至您的红包账户，可用于抵扣交易综合费。（注：盈利金额四舍五入取整后即为您的红包金额）",record.userProfit.p0fStringRoundCeil];
        }else if([record.profitFree isEqualWithDouble:0]){
            self.freeExLB.hidden = YES;
            self.totalMoneyLB.text = @"0.00";
            self.totalMoneyLB.textColor=[YZMAppStyle colorForLightGrayText];
        }else{
            self.freeExLB.hidden = YES;
            self.totalMoneyLB.text = record.profitFree.p02fString;
            self.totalMoneyLB.textColor=[YZMAppStyle colorForDropText];
        }
        
        if ([record.userProfit isGreaterThanDouble:0]) {
            
            self.resultStringLB.text = @"盈利分配";
            self.resultMoneyLB.text = [@"+" stringByAppendingString:record.userProfit.p02fString];
            self.resultMoneyLB.textColor = [YZMAppStyle colorForRiseText];
            
        }else if([record.userProfit isEqualWithDouble:0]){
            
            self.resultStringLB.text = @"盈利分配";
            self.resultMoneyLB.text = @"0.00";
            self.resultMoneyLB.textColor = [YZMAppStyle colorForLightGrayText];
            
        }else{
            
            self.resultStringLB.text = @"亏损赔付";
            self.resultMoneyLB.text = record.userProfit.p02fString;
            self.resultMoneyLB.textColor = [YZMAppStyle colorForDropText];
        }
    }else{
        self.amountLB.text = [NSString stringWithFormat:@"%@股",@(record.amount)];
        
        if ([@(record.totalProfit) isGreaterThanDouble:0]) {
            
            self.totalMoneyLB.text = [@"+" stringByAppendingString:@(record.totalProfit).p02fString];
            self.totalMoneyLB.textColor=[YZMAppStyle colorForRiseText];
        }else if([@(record.totalProfit) isEqualWithDouble:0]){
            
            self.totalMoneyLB.text = @"0.00";
            self.totalMoneyLB.textColor=[YZMAppStyle colorForLightGrayText];
        }else{
            self.totalMoneyLB.text = @(record.totalProfit).p02fString;
            self.totalMoneyLB.textColor=[YZMAppStyle colorForDropText];
        }
        
        if ([@(record.profit) isGreaterThanDouble:0]) {
            
            self.resultStringLB.text = @"盈利分配";
            self.resultMoneyLB.text = [@"+" stringByAppendingString:@(record.profit).p02fString];
            self.resultMoneyLB.textColor = [YZMAppStyle colorForRiseText];
            
        }else if([@(record.profit) isEqualWithDouble:0]){
            
            self.resultStringLB.text = @"盈利分配";
            self.resultMoneyLB.text = @"0.00";
            self.resultMoneyLB.textColor = [YZMAppStyle colorForLightGrayText];
            
        }else{
            
            self.resultStringLB.text = @"亏损赔付";
            self.resultMoneyLB.text = @(record.profit).p02fString;
            self.resultMoneyLB.textColor = [YZMAppStyle colorForDropText];
        }
    }
}

- (void)createObjects{
    
    _leftHelperView = [UIView new];
    [self.contentView addSubview:_leftHelperView];
    [_leftHelperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(30);
        make.centerY.equalTo(self.contentView);
    }];
    
    self.nameLB = [YZMAppStyle labelWithMediumWhite];
    [_leftHelperView addSubview:self.nameLB];
    [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(_leftHelperView);
    }];
    
    self.amountLB = [YZMAppStyle labelWithSmallLightGray];
    [_leftHelperView addSubview:self.amountLB];
    [self.amountLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(_leftHelperView);
        make.top.equalTo(self.nameLB.mas_bottom).offset(5);
    }];
    
    UILabel *totalStringLB = [YZMAppStyle labelWithMediumWhite];
    totalStringLB.text = @"交易盈利";
    [self.contentView addSubview:totalStringLB];
    [totalStringLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.nameLB);
    }];
    
    self.totalMoneyLB = [YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview:self.totalMoneyLB];
    [self.totalMoneyLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.amountLB);
    }];
    
    self.resultStringLB = [YZMAppStyle labelWithMediumWhite];
    [self.contentView addSubview:self.resultStringLB];
    [self.resultStringLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-30);
        make.top.equalTo(self.nameLB);
    }];
    
    self.resultMoneyLB = [YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview:self.resultMoneyLB];
    [self.resultMoneyLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.resultStringLB);
        make.top.equalTo(self.amountLB);
    }];
    
    self.freeExLB = [YZMAppStyle labelWithSmallLightGray];
    self.freeExLB.textColor = [YZMAppStyle colorForRiseText];
    self.freeExLB.numberOfLines = 0;
    self.freeExLB.hidden = YES;
    [self.contentView addSubview:self.freeExLB];
    [self.freeExLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(30);
        make.right.equalTo(self.contentView.mas_right).with.offset(-30);
        make.top.equalTo(self.amountLB.mas_bottom).with.offset(10);
    }];
}

@end
